package com.sogeti.mentormatch.aop.spring;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.sogeti.mentormatch.dao.DataAccessObject;
import com.sogeti.mentormatch.aop.DataAccessManager;

/**
 * @author Matthew L. Maurer <maurer.it@gmail.com>
 */
public class AbstractAccessManager<DAOBJECT extends DataAccessObject<DOBJECT, PKEY>, DOBJECT, PKEY extends Serializable>
    implements DataAccessManager<DOBJECT, PKEY>
{
    private DAOBJECT dataAccessObject;

    /**
     * 
     */
    @SuppressWarnings ( "unchecked" )
    public AbstractAccessManager ( ) {
        /*Class<?> clazz = getClass();
        ParameterizedType paramType = ( ParameterizedType ) clazz.getGenericSuperclass();
        Type[] types = paramType.getActualTypeArguments();
        
        if(types == null || types.length <= 0) {
            throw new IllegalArgumentException("Non-paramaterized instances can not be created.");
        }*/
    }

    public void setDataAccessObject ( DAOBJECT dataAccessObject ) {
        this.dataAccessObject = dataAccessObject;
    }

    public DAOBJECT getDataAccessObject ( ) {
        if ( this.dataAccessObject == null ) {
            throw new IllegalStateException(
                    "DataAccessObject was not set on Manager before usage");
        }

        return this.dataAccessObject;
    }

    /*
     * (non-Javadoc)
     * 
     * @see net.maurerit.jbudgetmanager.biz.DataAccessManager#create(java.lang.Object)
     */
    public PKEY create ( DOBJECT newInstance ) {
        return getDataAccessObject().create(newInstance);
    }

    /*
     * (non-Javadoc)
     * 
     * @see net.maurerit.jbudgetmanager.biz.DataAccessManager#delete(java.lang.Object)
     */
    public void delete ( DOBJECT persistentObject ) {
        getDataAccessObject().delete(persistentObject);
    }

    /*
     * (non-Javadoc)
     * 
     * @see net.maurerit.jbudgetmanager.biz.DataAccessManager#read(java.io.Serializable)
     */
    public DOBJECT read ( PKEY id ) {
        return getDataAccessObject().read(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see net.maurerit.jbudgetmanager.biz.DataAccessManager#update(java.lang.Object)
     */
    public void update ( DOBJECT transientObject ) {
        getDataAccessObject().update(transientObject);
    }

}
